moddbfandomcom-20200213-history
HGE:Tutorials:Input
=Input functions= Input_GetKey() returns the key code of the last pressed key. This is useful for "one-time" actions such as bringing up a menu or quitting the game. Input_GetKeyState() returns true if the specified key is down and false otherwise. This is useful for "continuous" actions such as player movement. See the following link for a complete list of key codes: HGE key code list In the following tutorial, we will implement usage of the arrow keys to move the "player" sprite. At the top of the program, add the following: float playerLocX = 200, playerLocY = 200; //the starting point of the player's location bool done = false; //is the program execution finished? In the FrameFunc, change the following statement playerSprite->Render(200, 200); //render the player sprite to playerSprite->Render(playerLocX , playerLocY); //render the player sprite This is done so that the player's location is no longer a constant value. We want to use the arrow keys for player movement and thus change the player's location when we press those keys. Also, in the FrameFunc, change ''' return false; to return done; We are going to use a boolean variable called done to determine whether to stop execution of the program. In the FrameFunc, above the '''hge->Gfx_BeginScene(); statement, add the following: //player movement if(hge->Input_GetKeyState(HGEK_UP)) playerLocY -= 100*dt; hge->Input_GetKeyState(HGEK_UP) returns true when the up arrow key is pressed and false otherwise. HGEK_UP is the key code for the up arrow key. When the up arrow key is pressed, the player sprite's Y-coordinate will decrease by 100 pixels per second. This is frame-rate independent movement: we are multiplying the time since the last call to FrameFunc (dt) by the rate at which we want to move the player, in pixels per second (100). Similarly, add the remaining functions for player movement: if(hge->Input_GetKeyState(HGEK_DOWN)) playerLocY += 100*dt; if(hge->Input_GetKeyState(HGEK_LEFT)) playerLocX -= 100*dt; if(hge->Input_GetKeyState(HGEK_RIGHT)) playerLocX += 100*dt; If the user presses the Esc key, we want to quit the program. To implement this, add the following to the FrameFunc: if(hge->Input_GetKey() HGEK_ESCAPE) done=true; //quit when Esc is pressed This is a "one-time" action (not continuous like player movement) so the Input_GetKey() function is used to check the key code of the last pressed key. HGEK_ESCAPE is the key code for the Escape key. Setting the done variable to true will stop execution of the program. =Using the mouse buttons= The mouse buttons act the same way as keyboard buttons. Simply use the following key codes for the mouse buttons. :HGEK_LBUTTON : Left mouse button :HGEK_RBUTTON : Right mouse button :HGEK_MBUTTON : Middle mouse button =Retrieving the mouse cursor location= Use the Input_GetMousePos() function to retrieve the current x and y coordinates of the mouse cursor location. At the beginning of the program, declare the following: float mouseX, mouseY; //coordinates of the mouse cursor At the start of the FrameFunc, get the current mouse cursor location by adding: hge->Input_GetMousePos(&mouseX, &mouseY); //get the current mouse position Note that the address of the variables have to be passed in. This will set mouseX and mouseY to the current position of the mouse cursor. Render some text that displays the mouse position by adding the following beneath the other text rendering functions: font1->printf(5, 75, "Mouse location: %.2f, %.2f", mouseX, mouseY); //render the current mouse position Finally, in the WinMain function, show the mouse cursor by setting a system state: hge->System_SetState(HGE_HIDEMOUSE, false); The HGE_HIDEMOUSE system state determines whether the mouse cursor is hidden. Setting it false means that the cursor will be displayed. The frame function should now look as follows: bool FrameFunc() { hge->Input_GetMousePos(&mouseX, &mouseY); //get the current mouse position float dt=hge->Timer_GetDelta(); //get the time since the last call to FrameFunc star->Update(dt); //update the animation //player movement if(hge->Input_GetKeyState(HGEK_UP)) playerLocY -= 100*dt; if(hge->Input_GetKeyState(HGEK_DOWN)) playerLocY += 100*dt; if(hge->Input_GetKeyState(HGEK_LEFT)) playerLocX -= 100*dt; if(hge->Input_GetKeyState(HGEK_RIGHT)) playerLocX += 100*dt; if(hge->Input_GetKey() HGEK_ESCAPE) done=true; //quit when Esc is pressed hge->Gfx_BeginScene(); hge->Gfx_Clear(0); //clear the screen, filling it with black bgSprite->RenderStretch(0, 0, 800, 600); //render the background sprite stretched playerSprite->Render(playerLocX , playerLocY); //render the player sprite star->Render(400, 300); //render the animation of a star font1->SetScale(1.0); //set text size to normal font1->SetColor(ARGB(255,0,0,0)); //set color of text to black font1->Render(5, 5, "This is some text"); //render text at coordinates 5, 5 int someNumber = 50; font1->SetScale(2.0); //set text size to twice its normal size font1->printf(5, 30, "Here is a number: %d", someNumber); //render text using printf-style formatting font1->SetScale(1.0); //set text size to normal font1->printf(5, 75, "Mouse location: %.2f, %.2f", mouseX, mouseY); //render the current mouse position hge->Gfx_EndScene(); return done; } Run the program and you can move the "player" sprite around with the arrow keys, and the current mouse cursor location will be displayed. We haven't done any collision checking, so the player is able to be moved off the screen. center You can download the source file and resource script file for this tutorial here: Tutorial 5 source, Resource script Next Section: Audio Category:C and HGE Category:HGE